package dbManage;

import invoiceManager.Invoice;
import invoiceManager.InvoiceBatch;
import invoiceManager.InvoicePanel;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

import editManager.Edit;
import editManager.EditRun;
import editManager.InWorkPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.io.File;

public class DBWindow {
	
	private MenuPanel managerPanel;
	private InvoicePanel invoicePanel;
	private InWorkPanel iwPanel;
	private JPanel holder;
	private DBListener dbL;
	private CardLayout cL;
	private Timer timer;
	private String currentTime, currentDate;
	private int dailyBilled;
	private File electronicCodes; //csv file
	
	private JFrame frmMariettaDraperyDatabase;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DBWindow window = new DBWindow();
					window.frmMariettaDraperyDatabase.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public DBWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		cL = new CardLayout();
		holder = new JPanel(cL);
		frmMariettaDraperyDatabase = new JFrame();
		frmMariettaDraperyDatabase.setPreferredSize(new Dimension(600, 400));
		frmMariettaDraperyDatabase.setContentPane(holder);
		frmMariettaDraperyDatabase.setTitle("Marietta Drapery Database");
		frmMariettaDraperyDatabase.setBounds(100, 100, 625, 425);
		frmMariettaDraperyDatabase.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		dbL = new DBListener();
		timer = new Timer(60000, dbL);
		timer.start();
		
		managerPanel = new MenuPanel();
		managerPanel.getDateLabel().setBounds(10, 11, 157, 18);
		managerPanel.getInvoiceButton().addActionListener(dbL);
		managerPanel.getInWorkButton().addActionListener(dbL);
		holder.add(managerPanel, "manager");
		updateClock();
		
		invoicePanel = new InvoicePanel();
		invoicePanel.getBtnBack().addActionListener(dbL);
		holder.add(invoicePanel, "invoice");
		
		iwPanel = new InWorkPanel();
		iwPanel.getBtnBack().addActionListener(dbL);
		holder.add(iwPanel, "in work");
		
		//for debug
		iwPanel.getBtnFindEdit().addActionListener(dbL);
		invoicePanel.getBtnFindInvoice().addActionListener(dbL);

	}
	
	private void updateClock(){
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
		SimpleDateFormat dayFormat = new SimpleDateFormat("MMMMMMMMMM dd, yyyy");
		currentTime = timeFormat.format(new Date());
		currentDate = dayFormat.format(new Date());
		managerPanel.getTimeLabel().setText(currentTime);
		managerPanel.getTimeLabel().repaint();
		managerPanel.getDateLabel().setText(currentDate);
		managerPanel.getDateLabel().repaint();
	}
	
	private void checkElectronicInvoices(InvoiceBatch iB) {
			ArrayList<Invoice> eList = invoicePanel.getCurrentBatch().getBatch();
			
	}



	private class DBListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(managerPanel.getInWorkButton())){
				cL.show(holder, "in work");
			}
			if(e.getSource().equals(managerPanel.getInvoiceButton())){
				cL.show(holder, "invoice");
			}
			if(e.getSource().equals(invoicePanel.getBtnBack()) || e.getSource().equals(iwPanel.getBtnBack())){
				cL.show(holder, "manager");
			}
			
			if(e.getSource().equals(iwPanel.getBtnFindEdit())){
				
			}
			//debug invoice
			if(e.getSource().equals(invoicePanel.getBtnFindInvoice())){
				
			}

		}




	}
}
